package com.lenovo.chartdemo;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.data.PieDataSet;

public class MainActivity extends AppCompatActivity {

    private ViewPager view_pager;
    private LinearLayout indicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        view_pager = (ViewPager) findViewById(R.id.view_pager);
        indicator = (LinearLayout) findViewById(R.id.indicator);
        view_pager.setAdapter(new ChartAdapter());
    }

    private String titles[] = new String[]{
      "LineChart",
      "BarChart",
      "PieChart",
      "StackedBarChart",
    };
    private class ChartAdapter extends PagerAdapter{
        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Chart chart = null;
            switch (position){
                case 0:
                    chart = LinearChartUtil.getChart(MainActivity.this);
                    break;
                case 1:
                    chart = BarChartUtil.getChart(MainActivity.this);
                    break;
                case 2:
                    chart = PieChartUtil.getChart(MainActivity.this);
                    break;
                case 3:
                    chart = StackedBarChartUtil.getChart(MainActivity.this);
                    break;
            }
            container.addView(chart);
            return chart;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
